package com.campus.dao.impl;

import com.campus.dao.StudentDao;
import com.campus.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
/**
 * @desc:
 * @author: Tournesol234
 * @date: 2024/11/22 14.41
 */
@Repository
public class StudentDaoImpl implements StudentDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int createStudent(Student student) {
        String sql = "INSERT INTO t_manager_student (stu_name, from_place, parent_phone, " +
                "current_grade, is_first, login_id, comment) VALUES (?, ?, ?, ?, ?, ?, ?)";
        return jdbcTemplate.update(sql, student.getStuName(), student.getFromPlace(),
                student.getParentPhone(), student.getCurrentGrade(), student.getIsFirst(),
                student.getLoginId(), student.getComment());
    }

    @Override
    public int updateStudent(int id, Student student) {
        String sql = "UPDATE t_manager_student SET stu_name = ?, from_place = ?, parent_phone = ?, current_grade = ?, is_first = ?, login_id = ?, comment = ? WHERE id = ?";
        return jdbcTemplate.update(sql, student.getStuName(), student.getFromPlace(), student.getParentPhone(), student.getCurrentGrade(), student.getIsFirst(), student.getLoginId(), student.getComment(), id);
    }

    @Override
    public int deleteStudent(int id) {
        String sql = "DELETE FROM t_manager_student WHERE id = ?";
        return jdbcTemplate.update(sql, id);
    }

    @Override
    public Student getStudentById(int id) {
        String sql = "SELECT * FROM t_manager_student WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(Student.class));
    }

    @Override
    public List<Student> getAllStudents() {
        String sql = "SELECT * FROM t_manager_student";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class));
    }

    @Override
    public int approveStudent(int id) {
        String sql = "UPDATE t_manager_student SET comment = 'Approved' WHERE id = ?";
        return jdbcTemplate.update(sql, id);
    }

    @Override
    public int batchApproveStudents(List<Integer> studentIds) {
        String sql = "UPDATE t_manager_student SET comment = 'Approved' WHERE id = ?";
        int result = 0;
        for (Integer id : studentIds) {
            result += jdbcTemplate.update(sql, id);
        }
        return result;
    }

    @Override
    public int batchCreateStudents(List<Student> students) {
        String sql = "INSERT INTO t_manager_student (stu_name, from_place, " +
                "parent_phone, current_grade, is_first, login_id, comment) VALUES (?, ?, ?, ?, ?, ?, ?)";
        int result = 0;
        for (Student student : students) {
            result += jdbcTemplate.update(sql, student.getStuName(),
                    student.getFromPlace(), student.getParentPhone(),
                    student.getCurrentGrade(), student.getIsFirst(), student.getLoginId(), student.getComment());
        }
        return result;
    }
}
